package Servlet;

import Dao.UserDao;
import Dao.medicineDao;
import bean.ReservationObject;
import bean.medicine;
import bean.medicineRecord;
import utils.Age;
import utils.Cookie_Session;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/medicineRecordServlet")
public class medicineRecordServlet extends BaseServlet{
    medicineDao medicineDao = new medicineDao();


    protected void ReservationInformation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");
        String phonenumber = req.getParameter("phonenumber");
        String idNumber = req.getParameter("IdNumber");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        int age = Age.AgeOfAcquisition(birthday);
        ReservationObject object = new ReservationObject(name,phonenumber,idNumber,sex,birthday,age);
        ServletContext context = req.getServletContext();
        context.setAttribute("reservation",object);
        req.getRequestDispatcher("/medicineServlet?action=list").forward(req,resp);
    }


/*
*   private medicine medicine;//预约药品对象
    private String rid;//记录序号
    private String pid;//病人id号
    private String ordertime;//预约时间

    private String state;//预约状态'预约状态：成功，取消，完成，爽约',

    int serialnumber ; // comment '取药序号',
*/


    //添加预约取药记录
    protected boolean addRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String localTime = Age.GetLocalTime(); //获取预约时间

        String rid;

        String mid = req.getParameter("mid"); //药品mid编号,获取药品信息
        medicine medicine = medicineDao.queryMedicineById(mid);

        Cookie cookie = Cookie_Session.findCookie("id", req.getCookies());//获取病人唯一id
        String pid = cookie.getValue();
        System.out.println(pid);
        //创建预约取药记录列表,并存放到session中
        HttpSession session = req.getSession();
        List<medicineRecord> recordList = (List<medicineRecord>) session.getAttribute("recordList");//从session中取出预约记录列表
        if (recordList == null) {//如果记录列表为空，则新建一个记录列表
            recordList = new ArrayList<medicineRecord>();
            session.setAttribute("recordList", recordList);
        }
        //检查是否存在相同地记录，如果存在相同记录，则添加失败
        if(recordList.size()==0){
            rid = "1";
        }
        else {
             rid = String.valueOf(Integer.parseInt(recordList.get(recordList.size() - 1).getRid()) + 1);
        }

        //rid记录序号等于上一个加1
        int serialnumber = recordList.size();
        String state = "成功";
        medicineRecord record = new medicineRecord(medicine,rid,pid,localTime,state,serialnumber);

        recordList.add(record);
        for (medicineRecord medicineRecord : recordList) {
            System.out.println(medicineRecord);
        }
        //recordList.add(this);//将新记录添加到列表中
        return true;
    }

    protected boolean removeRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        HttpSession session = req.getSession();
        List<medicineRecord> recordList = (List<medicineRecord>)session.getAttribute("recordList");//从session中取出预约记录列表
        if (recordList != null) {
            for (medicineRecord r : recordList) {
                if (r.getRid().equals(rid)) {
                    recordList.remove(r);//移除记录
                    return true;
                }
            }
        }
        return false;
    }
    protected boolean updateRecord(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        HttpSession session = req.getSession();
        List<medicineRecord> recordList = (List<medicineRecord>)session.getAttribute("recordList");//从session中取出预约记录列表

        return false;
    }
    protected void queryRecord (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询预约取药记录
            Cookie cookie = Cookie_Session.findCookie("id", req.getCookies());//获取病人唯一id
            String pid = cookie.getValue();
            System.out.println(" String pid = cookie.getValue();");
            HttpSession session = req.getSession();
            List<medicineRecord> recordList = (List<medicineRecord>)session.getAttribute("recordList");//从session中取出预约记录列表
            List<medicineRecord> result = new ArrayList<>();
            if (recordList != null) {//遍历记录列表，将符合条件的记录添加到结果中
                for (medicineRecord r : recordList) {
                    if (r.getPid().equals(pid)) {
                        result.add(r);
                    }
                }
            }
        for (medicineRecord record : result) {
            System.out.println(record);
        }

            System.out.println("病人号位"+pid);
            req.setAttribute("recordResult",result);
            req.getRequestDispatcher("/myAppointment.jsp").forward(req,resp);
        }
    }


